package cn.com.egova.erp.biz.impl;

import cn.com.egova.erp.biz.IReturnorderdetailBiz;
import cn.com.egova.erp.dao.IReturnorderdetailDao;
import cn.com.egova.erp.dao.IStoredetailDao;
import cn.com.egova.erp.dao.IStoreoperDao;
import cn.com.egova.erp.dao.ISupplierDao;
import cn.com.egova.erp.entity.*;
import cn.com.egova.erp.exception.ErpException;
import com.redsun.bos.ws.impl.IWaybillWs;

import java.util.Date;
import java.util.List;

/**
 * 退货订单明细业务逻辑类
 *
 * @author Administrator
 */
public class ReturnorderdetailBiz extends BaseBiz<Returnorderdetail> implements IReturnorderdetailBiz {

    private IReturnorderdetailDao returnorderdetailDao;
    private IStoredetailDao storedetailDao;
    private IStoreoperDao storeoperDao;
    private ISupplierDao supplierDao;
    private IWaybillWs waybillWs;

    public void setReturnorderdetailDao(IReturnorderdetailDao returnorderdetailDao) {
        this.returnorderdetailDao = returnorderdetailDao;
        super.setBaseDao(this.returnorderdetailDao);
    }

    public void setStoredetailDao(IStoredetailDao storedetailDao) {
        this.storedetailDao = storedetailDao;
    }

    public void setStoreoperDao(IStoreoperDao storeoperDao) {
        this.storeoperDao = storeoperDao;
    }

    public void setSupplierDao(ISupplierDao supplierDao) {
        this.supplierDao = supplierDao;
    }

    public void setWaybillWs(IWaybillWs waybillWs) {
        this.waybillWs = waybillWs;
    }

    @Override
    public void doOutStore(Long uuid, Long storeuuid, Long empuuid) {
        /**********************处理订单明细*********************/
        //1.获取订单明细
        Returnorderdetail detail = returnorderdetailDao.get(uuid);
        if (!Returnorderdetail.STATE_NOT_OUT.equals(detail.getState())) {
            throw new ErpException("不能重复出库");
        }
        //2.出库状态、库管员、出库时间、从哪个仓库出
        detail.setState(Returnorderdetail.STATE_OUT);
        detail.setEnder(empuuid);
        detail.setEndtime(new Date());
        detail.setStoreuuid(storeuuid);

        /**********************处理库存明细*********************/
        //构建查询条件
        Storedetail storedetail = new Storedetail();
        storedetail.setGoodsuuid(detail.getGoodsuuid());
        storedetail.setStoreuuid(storeuuid);

        //根据仓库编号和商品编号查询该仓库中是否有库存，如果没有则抛出异常，有则减库存。
        List<Storedetail> storeList = storedetailDao.getList(storedetail, null, null);
        if (storeList.size() > 0) {
            Storedetail exist = storeList.get(0);
            //exist.getNum()返回一个Long类型，如果为null，则拆箱会报错(实际调用的 longValue()报错)
            long num = exist.getNum() == null ? 0 : exist.getNum();
            if (num < detail.getNum()) {
                throw new ErpException("该商品库存不足(现存量:" + num + ")");
            } else {
                exist.setNum(num - detail.getNum());
            }
        } else {
            throw new ErpException("该商品没有库存");
        }


        /**********************处理操作记录*********************/
        Storeoper log = new Storeoper();
        log.setEmpuuid(empuuid);
        log.setGoodsuuid(detail.getGoodsuuid());
        log.setNum(detail.getNum());
        log.setStoreuuid(storeuuid);
        log.setOpertime(detail.getEndtime());
        log.setType(Storeoper.TYPE_OUT);
        storeoperDao.add(log);

        /**********************处理订单*********************/
        //查询订单下是否还存在明细状态为0的明细
        Returnorderdetail queryParam = new Returnorderdetail();
        Returnorders orders = detail.getReturnorders();
        queryParam.setReturnorders(orders);
        queryParam.setState(Returnorderdetail.STATE_NOT_OUT);
        long count = returnorderdetailDao.getCount(queryParam, null, null);
        if (count == 0) {
            //所有明细全部出库
            orders.setState(Returnorders.STATE_END);
            orders.setEndtime(detail.getEndtime());
            orders.setEnder(empuuid);

            //订单出库的时候，在线预约下单，运单号通过webservice服务得到。
            Supplier supplier = supplierDao.get(orders.getSupplieruuid());
            //参数1：客户ID(可任意指定，比如针对我们这个公司的客户物流公司指定客户ID为1)
            //参数2：收货地址
            //参数3：收货人
            //参数4：收货人电话
            //参数5：订单详情
            Long waybill = waybillWs.addWaybill(1l, supplier.getAddress(), supplier.getContact(), supplier.getTele(), "--");
            orders.setWaybillsn(waybill);
        }
    }


    /**
     * @param uuid      订单明细编号
     * @param storeuuid 仓库编号
     * @param empuuid   //库管员(登录人)编号
     */
    @Override
    public void doInStore(Long uuid, Long storeuuid, Long empuuid) {
        /**********************处理订单明细*********************/
        //1.获取订单明细
        Returnorderdetail detail = returnorderdetailDao.get(uuid);
        if (!Returnorderdetail.STATE_NOT_IN.equals(detail.getState())) {
            throw new ErpException("不能重复入库");
        }
        //2.入库状态、库管员、入库时间、入到哪个仓库
        detail.setState(Returnorderdetail.STATE_IN);
        detail.setEnder(empuuid);
        detail.setEndtime(new Date());
        detail.setStoreuuid(storeuuid);

        /**********************处理库存明细*********************/
        //构建查询条件
        Storedetail storedetail = new Storedetail();
        storedetail.setGoodsuuid(detail.getGoodsuuid());
        storedetail.setStoreuuid(storeuuid);

        //根据仓库编号和商品编号查询该仓库中是否已经有该商品存在，没有就插入新记录，有就累加库存。
        List<Storedetail> storeList = storedetailDao.getList(storedetail, null, null);

        //这里有一个知识点，如果存在，那么通过get方法拿到的是持久态的对象，当操作它的属性时，会自动更新到数据库；
        //如果不存在，那么我们new出来的storedetail对象是游离态，必须调用storedetailDao进行保存。
        if (storeList.size() > 0) {
            Storedetail exist = storeList.get(0);
            //exist.getNum()返回一个Long类型，如果为null，则拆箱会报错(实际调用的 longValue()报错)
            long num = exist.getNum() == null ? 0 : exist.getNum();
            exist.setNum(num + detail.getNum());
        } else {
            storedetail.setNum(detail.getNum());
            storedetailDao.add(storedetail);
        }


        /**********************处理操作记录*********************/
        Storeoper log = new Storeoper();
        log.setEmpuuid(empuuid);
        log.setGoodsuuid(detail.getGoodsuuid());
        log.setNum(detail.getNum());
        log.setStoreuuid(storeuuid);
        log.setOpertime(detail.getEndtime());
        log.setType(Storeoper.TYPE_IN);
        storeoperDao.add(log);

        /**********************处理订单*********************/
        //查询订单下是否还存在明细状态为0的明细
        Returnorderdetail queryParam = new Returnorderdetail();
        Returnorders orders = detail.getReturnorders();
        queryParam.setReturnorders(orders);
        queryParam.setState(Returnorderdetail.STATE_NOT_IN);
        long count = returnorderdetailDao.getCount(queryParam, null, null);
        if (count == 0) {
            //所有明细全部入库
            orders.setState(Returnorders.STATE_IN);
            orders.setEndtime(detail.getEndtime());
            orders.setEnder(empuuid);
        }
    }
}
